﻿using AiXiu.Common;
using AiXiu.DAL;
using AiXiu.IBLL;
using AiXiu.IDAL;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiXiu.BLL
{
    public class VideoBLL: IVideoBLL
    {
        /// <summary>
        /// 上传视频
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="fileName">文件名</param>
        /// <param name="headline">标题</param>
        /// <param name="location">位置</param>
        /// <returns></returns>
        public CreateUploadVideoResult ToBeUpload(int userId, string fileName, string headline, string location = null)
        {
            //获取视频的上传凭证
            VodHelper vodHelper = new VodHelper();
            CreateUploadVideoResult result=vodHelper.CreateUploadVideo(headline,fileName);

            //视频保存到数据库
            IVideoDAL videoDAL = new VideoDAL();
            TBVideos video = new TBVideos()
            {
                VideoId= result.VideoId,
                UserId= userId,
                Headline=headline,
                Location=location
            };
            videoDAL.AddVideo(video);
            return result;
        }

        /// <summary>
        /// 同步视频状态和封面地址
        /// </summary>
        /// <returns></returns>
        public int SyncVideos()
        {
            IVideoDAL videoDAL = new VideoDAL();
            List<string> ids= videoDAL.GetInProcessVideoIds();
            VodHelper vodHelper = new VodHelper();
            if(ids.Count>0)
            {
                GetVideoInfosResult result = vodHelper.GetVideoInfos(ids);
                videoDAL.UpdateVideos(result.VideoList);
            }            
            return ids.Count;

        }


        /// <summary>
        /// 通过用户id获取视频列表
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public List<TBVideos> GetVideosByUserId(int userId)
        {
            IVideoDAL videoDAL = new VideoDAL();
            return videoDAL.GetVideosByUserId(userId);
        }


        /// <summary>
        /// 根据视频id删除视频
        /// </summary>
        /// <param name="videoId"></param>
        public void DeleteVideo(string videoId)
        {
            //从数据删除视频
            IVideoDAL videoService = new VideoDAL();
            videoService.DeleteVideo(videoId);

            //从阿里云删除视频
            VodHelper vodHelper = new VodHelper();
            List<string> videoIds = new List<string>();
            videoIds.Add(videoId);
            vodHelper.DeleteVideos(videoIds);
        }

        /// <summary>
        /// 获取可播放的视频列表
        /// </summary>
        /// <returns></returns>
        public List<TBVideos> GetVideos()
        {
            IVideoDAL videoDAL = new VideoDAL();
            return videoDAL.GetVideos();
        }

        /// <summary>
        /// 根据视频id获取视频信息
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public TBVideos GetVideoByVideoId(string videoId)
        {
            IVideoDAL videoDAL = new VideoDAL();
            return videoDAL.GetVideoByVideoId(videoId);
        }

        /// <summary>
        /// 初始化播放
        /// </summary>
        /// <returns></returns>
        public GetPlayInfoResult Initplay(string videoId)
        {
            VodHelper vodHelper = new VodHelper();
            return vodHelper.GetPlayInfo(videoId);
        }
    }
}
